# Bazel build
# C/C++ documentation: https://docs.bazel.build/versions/master/be/c-cpp.html

load("@rules_proto_grpc//python:defs.bzl", "python_proto_compile")
load("//bazel:ray.bzl", "COPTS")

proto_library(
    name = "streaming_proto",
    srcs = ["src/protobuf/streaming.proto"],
    visibility = ["//visibility:public"],
)

proto_library(
    name = "streaming_queue_proto",
    srcs = ["src/protobuf/streaming_queue.proto"],
)

proto_library(
    name = "remote_call_proto",
    srcs = ["src/protobuf/remote_call.proto"],
    visibility = ["//visibility:public"],
    deps = ["streaming_proto"],
)

cc_proto_library(
    name = "streaming_cc_proto",
    deps = [":streaming_proto"],
)

cc_proto_library(
    name = "streaming_queue_cc_proto",
    deps = ["streaming_queue_proto"],
)

# Use `linkshared` to ensure ray related symbols are not packed into streaming libs
# to avoid duplicate symbols. In runtime we expose ray related symbols, which can
# be linked into streaming libs by dynamic linker. See bazel rule `//:_raylet`
cc_binary(
    name = "ray_util.so",
    copts = COPTS,
    linkshared = 1,
    visibility = ["//visibility:public"],
    deps = ["//:ray_util"],
)

cc_binary(
    name = "ray_common.so",
    copts = COPTS,
    linkshared = 1,
    visibility = ["//visibility:public"],
    deps = ["//:ray_common"],
)

cc_binary(
    name = "core_worker_lib.so",
    copts = COPTS,
    linkshared = 1,
    deps = ["//:core_worker_lib"],
)

cc_library(
    name = "streaming_util",
    srcs = glob([
        "src/util/*.cc",
    ]),
    hdrs = glob([
        "src/util/*.h",
    ]),
    copts = COPTS,
    includes = [
        "src",
    ],
    visibility = ["//visibility:public"],
    deps = [
        "ray_util.so",
        "@boost//:any",
        "@com_google_googletest//:gtest",
    ],
)

cc_library(
    name = "streaming_config",
    srcs = glob([
        "src/config/*.cc",
    ]),
    hdrs = glob([
        "src/config/*.h",
    ]),
    copts = COPTS,
    deps = [
        "ray_common.so",
        ":streaming_cc_proto",
        ":streaming_util",
    ],
)

cc_library(
    name = "streaming_message",
    srcs = glob([
        "src/message/*.cc",
    ]),
    hdrs = glob([
        "src/message/*.h",
    ]),
    copts = COPTS,
    deps = [
        "ray_common.so",
        ":streaming_config",
        ":streaming_util",
    ],
)

cc_library(
    name = "streaming_queue",
    srcs = glob([
        "src/queue/*.cc",
    ]),
    hdrs = glob([
        "src/queue/*.h",
    ]),
    copts = COPTS,
    deps = [
        "ray_common.so",
        "ray_util.so",
        ":streaming_config",
        ":streaming_message",
        ":streaming_queue_cc_proto",
        ":streaming_util",
        "@boost//:asio",
        "@boost//:thread",
    ] + select({
        "@bazel_tools//src/conditions:windows": [
            # TODO(mehrdadn): This is to resolve symbols on Windows for now. Should remove this later. (See d7f8d18.)
            "//:core_worker_lib",
        ],
        "//conditions:default": [
            "core_worker_lib.so",
        ],
    }),
)

cc_library(
    name = "streaming_lib",
    srcs = glob([
        "src/*.cc",
    ]),
    hdrs = glob([
        "src/*.h",
        "src/queue/*.h",
        "src/test/*.h",
    ]),
    copts = COPTS,
    includes = ["src"],
    visibility = ["//visibility:public"],
    deps = [
        "ray_common.so",
        "ray_util.so",
        ":streaming_config",
        ":streaming_message",
        ":streaming_queue",
        ":streaming_util",
        "@boost//:circular_buffer",
    ],
)

test_common_deps = [
    ":streaming_lib",
    "//:ray_common",
    "//:ray_util",
    "//:core_worker_lib",
]

# streaming queue mock actor binary
cc_binary(
    name = "streaming_test_worker",
    srcs = glob(["src/test/*.h"]) + [
        "src/test/mock_actor.cc",
    ],
    copts = COPTS,
    includes = [
        "streaming/src/test",
    ],
    deps = test_common_deps,
)

# use src/test/run_streaming_queue_test.sh to run this test
cc_binary(
    name = "streaming_queue_tests",
    srcs = glob(["src/test/*.h"]) + [
        "src/test/streaming_queue_tests.cc",
    ],
    copts = COPTS,
    deps = test_common_deps,
)

cc_test(
    name = "streaming_message_ring_buffer_tests",
    srcs = [
        "src/test/ring_buffer_tests.cc",
    ],
    copts = COPTS,
    includes = [
        "streaming/src/test",
    ],
    deps = test_common_deps,
)

cc_test(
    name = "streaming_message_serialization_tests",
    srcs = [
        "src/test/message_serialization_tests.cc",
    ],
    copts = COPTS,
    deps = test_common_deps,
)

cc_test(
    name = "streaming_mock_transfer",
    srcs = [
        "src/test/mock_transfer_tests.cc",
    ],
    copts = COPTS,
    deps = test_common_deps,
)

cc_test(
    name = "streaming_util_tests",
    srcs = [
        "src/test/streaming_util_tests.cc",
    ],
    copts = COPTS,
    deps = test_common_deps,
)

cc_test(
    name = "event_service_tests",
    srcs = [
        "src/test/event_service_tests.cc",
    ],
    deps = test_common_deps,
)

python_proto_compile(
    name = "streaming_py_proto",
    deps = ["//streaming:streaming_proto"],
)

python_proto_compile(
    name = "remote_call_py_proto",
    deps = ["//streaming:remote_call_proto"],
)

filegroup(
    name = "all_py_proto",
    srcs = [
        ":remote_call_py_proto",
        ":streaming_py_proto",
    ],
)

genrule(
    name = "copy_streaming_py_proto",
    srcs = [
        ":all_py_proto",
    ],
    outs = [
        "copy_streaming_py_proto.out",
    ],
    cmd = """
        set -e
        set -x
        WORK_DIR="$$(pwd)"
        # Copy generated files.
        GENERATED_DIR="$$WORK_DIR/streaming/python/generated"
        rm -rf "$$GENERATED_DIR"
        mkdir -p "$$GENERATED_DIR"
        touch "$$GENERATED_DIR/__init__.py"
        for f in $(locations //streaming:all_py_proto); do
            cp -f "$$f" "$$GENERATED_DIR"
        done
        sed -i -E 's/from streaming.src.protobuf/from ./' "$$GENERATED_DIR/remote_call_pb2.py"
        date > $@
    """,
    local = 1,
    visibility = ["//visibility:public"],
)

# Streaming java
genrule(
    name = "copy_jni_h",
    srcs = ["@bazel_tools//tools/jdk:jni_header"],
    outs = ["jni.h"],
    cmd = "cp -f $< $@",
)

genrule(
    name = "copy_jni_md_h",
    srcs = select({
        "@bazel_tools//src/conditions:windows": ["@bazel_tools//tools/jdk:jni_md_header-windows"],
        "@bazel_tools//src/conditions:darwin": ["@bazel_tools//tools/jdk:jni_md_header-darwin"],
        "//conditions:default": ["@bazel_tools//tools/jdk:jni_md_header-linux"],
    }),
    outs = ["jni_md.h"],
    cmd = "cp -f $< $@",
    visibility = ["//visibility:public"],
)

cc_library(
    name = "jni",
    hdrs = [
        ":jni.h",
        ":jni_md.h",
    ],
    copts = COPTS,
    includes = [
        ".",  # needed for `include <jni.h>`
    ],
    visibility = ["//visibility:public"],
)

cc_binary(
    name = "libstreaming_java.so",
    srcs = glob([
        "src/lib/java/*.cc",
        "src/lib/java/*.h",
    ]),
    copts = COPTS,
    includes = [
        "src",
    ],
    linkshared = 1,
    linkstatic = 1,
    visibility = ["//visibility:public"],
    deps = [
        ":jni",
        ":streaming_lib",
    ],
)
